﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.Assert;
using CommunitySystem.Ctrls;
using CommunitySystem.EmpbasicService;
using CommunitySystem.GuaZhiDuanLianService;
using CommunitySystem.Views;
using EHR.Common;

namespace CommunitySystem
{
    public partial class GuaZhiEmpControl : AsyncCtrl, IReturnListenerc
    {
        #region IReturnListenerc 成员
        public void BeginReturn()
        {
           
        }
        #endregion
        public NavMoveSwitch Nav { get; set; }
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        public ObservableCollection<EmpbasicService.OrgForHRModel> OrgList =
            new ObservableCollection<EmpbasicService.OrgForHRModel>();
        ObservableCollection<EmpbasicService.OrgForHRModel> forumList = new ObservableCollection<EmpbasicService.OrgForHRModel>();
        public ObservableCollection<GuaZhiEmpModel> selectedempmodel =
            new ObservableCollection<GuaZhiEmpModel>();
        private GuaZhiDuanLianServiceClient Client1 =
            new GuaZhiDuanLianServiceClient();
        private ShiTi shiti = new ShiTi();
        public GuaZhiEmpControl()
        {
            InitializeComponent();
            ListBoxOfLife.Items.Clear();
            jiguan.OnSelectionChanged += new OnSelectionChangedEvent(jiguan_OnSelectionChanged);
            Client.GetDengLuJueSeCompleted += new EventHandler<EmpbasicService.GetDengLuJueSeCompletedEventArgs>(Client_GetDengLuJueSeCompleted);
            Client.GetDengLuJueSeAsync(Session.LoginUser.LoginName);
            Client.GetOrgForHRModelCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelCompletedEventArgs>(Client_GetOrgForHRModelCompleted);
            Client.GetOrgForHRModelbyIDsCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs>(Client_GetOrgForHRModelbyIDsCompleted);
            Client.GetDengLuOrgCompleted += new EventHandler<GetDengLuOrgCompletedEventArgs>(Client_GetDengLuOrgCompleted);
            Client1.GetGuaZhiDuanLianEmpModelCompleted += new EventHandler<GetGuaZhiDuanLianEmpModelCompletedEventArgs>(Client1_GetGuaZhiDuanLianEmpModelCompleted);
            Client1.GetGuaZhiDuanLianEmpZongModelCompleted += 
                new EventHandler<GetGuaZhiDuanLianEmpZongModelCompletedEventArgs>(Client1_GetGuaZhiDuanLianEmpZongModelCompleted);
            Client1.GetGuaZhiDuanLianJiHuaModelCompleted += new EventHandler<GetGuaZhiDuanLianJiHuaModelCompletedEventArgs>(Client1_GetGuaZhiDuanLianJiHuaModelCompleted);
            Client1.GetGuaZhiDuanLianJiHuaModelAsync(0, "0",0);
            Client1.CheckGuaZhiEmpCompleted += new EventHandler<CheckGuaZhiEmpCompletedEventArgs>(Client1_CheckGuaZhiEmpCompleted);
            Client1.CancelGuaZhiEmpCompleted += new EventHandler<CancelGuaZhiEmpCompletedEventArgs>(Client1_CancelGuaZhiEmpCompleted);
            Client1.TuiGuaZhiEmpCompleted += new EventHandler<TuiGuaZhiEmpCompletedEventArgs>(Client1_TuiGuaZhiEmpCompleted);
            Client1.UpdateShiJianCompleted += new EventHandler<UpdateShiJianCompletedEventArgs>(Client1_UpdateShiJianCompleted);
            Client1.ShenHeShenHeYanQiGuaZhiEmpCompleted += new EventHandler<ShenHeShenHeYanQiGuaZhiEmpCompletedEventArgs>(Client1_ShenHeShenHeYanQiGuaZhiEmpCompleted);
            Client1.GetGuaZhiDuanLianJiHuaModelForComCompleted += 
                new EventHandler<GetGuaZhiDuanLianJiHuaModelForComCompletedEventArgs>(Client1_GetGuaZhiDuanLianJiHuaModelForComCompleted);
            Client1.GetGuaZhiDuanLianJiHuaModelForComAsync("");
            shiti.ygzt = "未审核";
            jiguan.DataContext = shiti;
            DGRole.Columns[3].Visibility =
                    DGRole.Columns[4].Visibility =
                    DGRole.Columns[5].Visibility = DGRole.Columns[6].Visibility = DGRole.Columns[21].Visibility =
                        DGRole.Columns[22].Visibility =
                        DGRole.Columns[23].Visibility =
                        DGRole.Columns[24].Visibility = System.Windows.Visibility.Collapsed;
            yanqi.Visibility = zhongzhi.Visibility = System.Windows.Visibility.Collapsed;
        }

        void Client1_GetGuaZhiDuanLianJiHuaModelForComCompleted(object sender, GetGuaZhiDuanLianJiHuaModelForComCompletedEventArgs e)
        {
            biaoti.ItemsSource = e.Result;
            if (e.Result != null && e.Result.Count > 0)
                biaoti.SelectedIndex = 0;
        }

        void Client1_UpdateShiJianCompleted(object sender, UpdateShiJianCompletedEventArgs e)
        {
            if (count == selectedempmodel.Count)
            {
                count = count - 1;
                SilverlightMessageBoxLibrary.Message.InfoMessage("安排成功！");
                string ss = "";
                foreach (string ii in orgids)
                    ss += ii + ",";
                if (string.IsNullOrEmpty(ss)) ss = "0";
                else ss = ss.Substring(0, ss.Length - 1);
                LoadData(ss);
            }
        }

        void Client1_TuiGuaZhiEmpCompleted(object sender, TuiGuaZhiEmpCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("退回成功！");
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }

        void Client1_CancelGuaZhiEmpCompleted(object sender, CancelGuaZhiEmpCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("取消审核成功！");
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }

        void Client1_CheckGuaZhiEmpCompleted(object sender, CheckGuaZhiEmpCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("审核成功！");
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }


        #region 获得组织机构树
        void Client_GetOrgForHRModelCompleted(object sender, EmpbasicService.GetOrgForHRModelCompletedEventArgs e)
        {
            OrgList = e.Result;
            forumList.Clear();
            foreach (EmpbasicService.OrgForHRModel i in e.Result)
            {
                forumList.Add(i);
            }
            AddTreeNode("全部单位", null);
        }

        private void AddTreeNode(string type, TreeViewItem treeViewItem)
        {
            List<EmpbasicService.OrgForHRModel> result = (from forumInfo in forumList
                                                          where forumInfo.Type == type
                                                          select forumInfo).ToList<EmpbasicService.OrgForHRModel>();

            if (result.Count > 0)
            {
                foreach (EmpbasicService.OrgForHRModel foruminfo in result)
                {
                    TreeViewItem objTreeNode = new TreeViewItem();

                    //StackPanel sp = new StackPanel();
                    CheckBox cb = new CheckBox();
                    cb.Content = foruminfo.Name;
                    cb.Tag = objTreeNode;
                    cb.DataContext = foruminfo;
                    //sp.Children.Add(cb);
                    cb.Click += new RoutedEventHandler(cb_Checked);
                    objTreeNode.Header = cb;
                    //objTreeNode.Header = foruminfo.Name;
                    //objTreeNode.DataContext = foruminfo;
                    //objTreeNode.Tag = foruminfo.ID;
                    if ((objTreeNode.Header as CheckBox).Content.ToString() == "组织机构") objTreeNode.IsExpanded = true;

                    //此样式将会添加的所有叶子结点上
                    //objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style;

                    //添加根节点
                    if (treeViewItem == null)
                    {
                        TreeOfLife.Items.Add(objTreeNode);
                    }
                    else
                    {
                        treeViewItem.Items.Add(objTreeNode);
                    }
                    AddTreeNode(foruminfo.Name, objTreeNode);
                }
            }
        }
        public int orgid { get; set; }
        public List<string> orgids = new List<string>();
        public string type { get; set; }
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            EmpbasicService.OrgForHRModel rm = cb.DataContext as EmpbasicService.OrgForHRModel;
            if (cb.IsChecked == true)
            {
                //if (orgids != null)
                //{
                //orgid = Convert.ToInt32(((EmpbasicService.OrgForHRModel)cb.DataContext).ID);
                //tbTitle.Text = cb.Content.ToString();
                if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                    orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                if (string.IsNullOrEmpty(ss) || orgids.Contains("79")) ss = "0";
                else ss = ss.Substring(0, ss.Length - 1);
                LoadData(ss);
                CheckChildItem(cb.Tag as TreeViewItem);
            }
            else
            {
                //if (orgids != null)
                //{
                if (orgids != null)
                {
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                }
                string ss = "";
                foreach (string i in orgids)
                    ss += i + ",";
                if (ss.Length > 1) ss = ss.Substring(0, ss.Length - 1);
                LoadData(ss);
                UnCheckChildItem(cb.Tag as TreeViewItem);
            }
        }

        void CheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = true;
                    if (!orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Add(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    CheckChildItem(tvi);
                }
            }
        }
        void UnCheckChildItem(TreeViewItem item)
        {
            foreach (TreeViewItem tvi in item.Items)
            {
                if (tvi != null)
                {
                    CheckBox cb = tvi.Header as CheckBox;
                    cb.IsChecked = false;
                    if (orgids.Contains(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString()))
                        orgids.Remove(((EmpbasicService.OrgForHRModel)cb.DataContext).ID.ToString());
                    UnCheckChildItem(tvi);
                }
            }
        }
        #endregion
        #region 左右拖动的条
        private GridLength _lastColLeftLength;
        private GridLength _lastColRightLength;
        private void GridSpliteTo_ToLeft(object sender, EventArgs e)
        {
            //已经到最左边，直接返回
            if (colLeft.Width.Value == 0.0)
            {
                return;
            }

            //
            if (colRight.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colLeft.Width = new GridLength(0);
                colRight.Width = new GridLength(150, GridUnitType.Star);
            }
        }

        private void GridSpliteTo_ToRight(object sender, EventArgs e)
        {
            if (colRight.Width.Value == 0)
            {
                return;
            }

            if (colLeft.Width.Value == 0)
            {
                colLeft.Width = _lastColLeftLength;
                colRight.Width = _lastColRightLength;
            }
            else
            {
                _lastColLeftLength = colLeft.Width;
                _lastColRightLength = colRight.Width;

                colRight.Width = new GridLength(0);
                colLeft.Width = new GridLength(300, GridUnitType.Star);
            }
        }
        #endregion
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            orgid = Convert.ToInt32(((OrgForHRModel)(ListBoxOfLife.SelectedItem)).ID);
            orgids.Clear();
            orgids.Add(orgid.ToString());
            LoadData(orgids[0]);
        }

        void Client_GetOrgForHRModelbyIDsCompleted(object sender, EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs e)
        {
            ListBoxOfLife.ItemsSource = e.Result;
            ListBoxOfLife.SelectedIndex = 0;
            orgid = Convert.ToInt32(e.Result[0].ID);
            orgids.Clear();
            orgids.Add(orgid.ToString());
            LoadData(orgids[0]);
        }

        private string Xuanxiang = "未审核";
        void jiguan_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            Xuanxiang = NewValue.ToString();
            //YanZheng(juese, Xuanxiang);
        }
        public void loadyanzheng(string juese,string xuanxiang)
        {
            if (juese != "人事干部")
            {
                if (xuanxiang == "挂职中")
                {
                    DGRole.Columns[3].Visibility =
                        DGRole.Columns[4].Visibility =
                            DGRole.Columns[5].Visibility =
                             DGRole.Columns[6].Visibility = System.Windows.Visibility.Visible;
                    DGRole.Columns[21].Visibility =
                        DGRole.Columns[22].Visibility = System.Windows.Visibility.Collapsed;
                }
                else if (xuanxiang == "挂职中-延期" || xuanxiang == "挂职中-延期申请")
                {
                    DGRole.Columns[3].Visibility =
                        DGRole.Columns[4].Visibility =
                            DGRole.Columns[5].Visibility =
                            DGRole.Columns[21].Visibility =
                            DGRole.Columns[22].Visibility = DGRole.Columns[6].Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    DGRole.Columns[3].Visibility =
                        DGRole.Columns[4].Visibility =
                        DGRole.Columns[5].Visibility =
                        DGRole.Columns[21].Visibility =
                            DGRole.Columns[22].Visibility = DGRole.Columns[6].Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            else
            {
                if (xuanxiang == "挂职中")
                {
                    DGRole.Columns[3].Visibility =
                        DGRole.Columns[4].Visibility =
                            DGRole.Columns[5].Visibility =
                             DGRole.Columns[6].Visibility = System.Windows.Visibility.Visible;
                    DGRole.Columns[21].Visibility =
                        DGRole.Columns[22].Visibility =
                        DGRole.Columns[23].Visibility =
                        DGRole.Columns[24].Visibility = System.Windows.Visibility.Collapsed;

                }
                else if (xuanxiang == "挂职中-延期")
                {
                    DGRole.Columns[3].Visibility =
                        DGRole.Columns[4].Visibility =
                            DGRole.Columns[5].Visibility =
                            DGRole.Columns[21].Visibility =
                            DGRole.Columns[22].Visibility =
                            DGRole.Columns[23].Visibility =
                            DGRole.Columns[24].Visibility = DGRole.Columns[6].Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    DGRole.Columns[3].Visibility =
                        DGRole.Columns[4].Visibility =
                        DGRole.Columns[5].Visibility =
                        DGRole.Columns[21].Visibility =
                            DGRole.Columns[22].Visibility =
                            DGRole.Columns[23].Visibility =
                            DGRole.Columns[24].Visibility = DGRole.Columns[6].Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }
        public void YanZheng(string juese,string xuanxiang)
        {
            if(juese != "人事干部")
            {
                newrenyuan.Visibility = System.Windows.Visibility.Collapsed;
                SavePeiZhi1.Visibility = System.Windows.Visibility.Collapsed;
                CheckData.Visibility = System.Windows.Visibility.Visible;
                CancelCheckData.Visibility = System.Windows.Visibility.Visible;
                TuiData.Visibility = System.Windows.Visibility.Visible;
                SavePeiZhi.Visibility = System.Windows.Visibility.Visible;
                yanqi.Visibility = System.Windows.Visibility.Visible;
                zhongzhi.Visibility = System.Windows.Visibility.Visible;
                shenheyanqi.Visibility = System.Windows.Visibility.Visible;
                if (xuanxiang == "挂职中")
                {
                    CheckData.Visibility = System.Windows.Visibility.Collapsed;
                    CancelCheckData.Visibility = System.Windows.Visibility.Collapsed;
                    TuiData.Visibility = System.Windows.Visibility.Collapsed;
                    SavePeiZhi.Visibility = System.Windows.Visibility.Collapsed;
                    

                }
                else if (xuanxiang == "挂职中-延期" || xuanxiang == "挂职中-延期申请")
                {
                    CheckData.Visibility = System.Windows.Visibility.Collapsed;
                    CancelCheckData.Visibility = System.Windows.Visibility.Collapsed;
                    TuiData.Visibility = System.Windows.Visibility.Collapsed;
                    SavePeiZhi.Visibility = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    yanqi.Visibility = System.Windows.Visibility.Collapsed;
                    zhongzhi.Visibility = System.Windows.Visibility.Collapsed;
                    shenheyanqi.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            else
            {
                newrenyuan.Visibility = System.Windows.Visibility.Visible;
                SavePeiZhi1.Visibility = System.Windows.Visibility.Visible;
                CheckData.Visibility = System.Windows.Visibility.Collapsed;
                CancelCheckData.Visibility = System.Windows.Visibility.Collapsed;
                TuiData.Visibility = System.Windows.Visibility.Collapsed;
                SavePeiZhi.Visibility = System.Windows.Visibility.Collapsed;
                yanqi.Visibility = System.Windows.Visibility.Visible;
                zhongzhi.Visibility = System.Windows.Visibility.Collapsed;
                shenheyanqi.Visibility = System.Windows.Visibility.Collapsed;
                if (xuanxiang == "挂职中")
                {
                    CheckData.Visibility = System.Windows.Visibility.Collapsed;
                    CancelCheckData.Visibility = System.Windows.Visibility.Collapsed;
                    TuiData.Visibility = System.Windows.Visibility.Collapsed;
                    SavePeiZhi.Visibility = System.Windows.Visibility.Collapsed;
                    DGRole.Columns[3].Visibility =
                        DGRole.Columns[4].Visibility =
                            DGRole.Columns[5].Visibility =
                             DGRole.Columns[6].Visibility = System.Windows.Visibility.Visible;
                    DGRole.Columns[21].Visibility =
                        DGRole.Columns[22].Visibility =
                        DGRole.Columns[23].Visibility =
                        DGRole.Columns[24].Visibility = System.Windows.Visibility.Collapsed;

                }
                else if(xuanxiang == "挂职中-延期")
                {
                    CheckData.Visibility = System.Windows.Visibility.Collapsed;
                    CancelCheckData.Visibility = System.Windows.Visibility.Collapsed;
                    TuiData.Visibility = System.Windows.Visibility.Collapsed;
                    SavePeiZhi.Visibility = System.Windows.Visibility.Collapsed;
                    DGRole.Columns[3].Visibility =
                        DGRole.Columns[4].Visibility =
                            DGRole.Columns[5].Visibility =
                            DGRole.Columns[21].Visibility =
                            DGRole.Columns[22].Visibility =
                            DGRole.Columns[23].Visibility =
                            DGRole.Columns[24].Visibility = DGRole.Columns[6].Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    yanqi.Visibility = System.Windows.Visibility.Collapsed;
                    zhongzhi.Visibility = System.Windows.Visibility.Collapsed;
                    shenheyanqi.Visibility = System.Windows.Visibility.Collapsed;
                    DGRole.Columns[3].Visibility =
                        DGRole.Columns[4].Visibility =
                        DGRole.Columns[5].Visibility =
                        DGRole.Columns[21].Visibility =
                            DGRole.Columns[22].Visibility =
                            DGRole.Columns[23].Visibility =
                            DGRole.Columns[24].Visibility = DGRole.Columns[6].Visibility = System.Windows.Visibility.Collapsed;
                }
            }
        }

        private string juese = "";
        void Client_GetDengLuJueSeCompleted(object sender, EmpbasicService.GetDengLuJueSeCompletedEventArgs e)
        {
            juese = e.Result;
            YanZheng(juese, Xuanxiang);
            if (e.Result != "人事干部")
            {
                if (TreeOfLife.ItemsSource != null) TreeOfLife.ItemsSource = null;
                TreeOfLife.Visibility = System.Windows.Visibility.Visible;
                ListBoxOfLife.Visibility = System.Windows.Visibility.Collapsed;
                Client.GetOrgForHRModelAsync(0);
                LoadData("0");
            }
            else
            {
                TreeOfLife.Visibility = System.Windows.Visibility.Collapsed;
                ListBoxOfLife.Visibility = System.Windows.Visibility.Visible;
                Client.GetDengLuOrgAsync(Session.LoginUser.LoginName);
            }
        }
        public String[] Oldorgids;
        void Client_GetDengLuOrgCompleted(object sender, EmpbasicService.GetDengLuOrgCompletedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.Result))
            {
                Oldorgids = e.Result.Split(',');
            }
            Client.GetOrgForHRModelbyIDsAsync("(" + e.Result + ")");
        }
        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }

        void Client1_GetGuaZhiDuanLianJiHuaModelCompleted(object sender, GetGuaZhiDuanLianJiHuaModelCompletedEventArgs e)
        {
            biaoti.ItemsSource = e.Result;
        }
        private string islock = "";
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            biaoti.ItemsSource = null;
            islock = "已结束";
            Client1.GetGuaZhiDuanLianJiHuaModelAsync(0, islock, 0);
            Client1.GetGuaZhiDuanLianJiHuaModelForComAsync("已结束");
        }

        private void chkHiddenCols_Unchecked(object sender, RoutedEventArgs e)
        {
            biaoti.ItemsSource = null;
            islock = "";
            Client1.GetGuaZhiDuanLianJiHuaModelAsync(0, islock, 0);
            Client1.GetGuaZhiDuanLianJiHuaModelForComAsync("");
        }
        private int ExamID = 0;
        private string kaoshistatus = "";
        void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                ExamID = ((GuaZhiDuanLianJiHuaModel)((((ComboBox)sender).SelectedItem))).ID;
                kaoshistatus =
                    ((GuaZhiDuanLianJiHuaModel)
                     ((((ComboBox)sender).SelectedItem))).Status;
                string ss = "";
                foreach (string ii in orgids)
                    ss += ii + ",";
                if (string.IsNullOrEmpty(ss)) ss = "0";
                else ss = ss.Substring(0, ss.Length - 1);
                LoadData(ss);
            }
            else ExamID = 0;
        }

        public void LoadData(string orgids)
        {
            //Client1.GetGuaZhiDuanLianJiHuaModelForComAsync("");
            Client1.GetGuaZhiDuanLianEmpZongModelAsync(ExamID, orgids, shiti.ygzt,null);
        }

        void Client1_GetGuaZhiDuanLianEmpZongModelCompleted(object sender, GetGuaZhiDuanLianEmpZongModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = e.Result;
            loadyanzheng(juese, Xuanxiang);
            YanZheng(juese, Xuanxiang);
        }
        private void NewData_Click1(object sender, RoutedEventArgs e)
        {
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            AddGuaZhiEmpWindow RMWindow1 = new AddGuaZhiEmpWindow(ss,ExamID);
            RMWindow1.Closed += new EventHandler(win_Closed);
            RMWindow1.Show();
        }


        void Client1_GetGuaZhiDuanLianEmpModelCompleted(object sender, GetGuaZhiDuanLianEmpModelCompletedEventArgs e)
        {
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            foreach (GuaZhiEmpModel p in DGRole.ItemsSource)
            {
                p.IsSelected = (bool)cb.IsChecked;
                //CheckBox cb1 = DGRole.Columns[0].GetCellContent(p) as CheckBox;
                //cb1.IsChecked = p.IsSelected;
                if (p.IsSelected)
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(p);
                    }
                }
                else
                {
                    if (selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault() != null)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(i => i.ID == p.ID).FirstOrDefault());
                    }
                }
            }

        }

        private void CheckData_Click(object sender, RoutedEventArgs e)
        {
            if (kaoshistatus == "已结束")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选计划状态不可进行审核通过操作！");
                return;
            }
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (GuaZhiEmpModel i in (ObservableCollection<GuaZhiEmpModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    if (i.Status == "审核通过" || i.Status == "终止")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选信息状态为不可审核状态！");
                        return;
                    }
                }
                if (MessageBox.Show("确定通过审核吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";

                    foreach (GuaZhiEmpModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    Client1.CheckGuaZhiEmpAsync("(" + ids.Substring(0, ids.Length - 1) + ")");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要通过审核的信息！");
        }

        private void CancelCheckData_Click(object sender, RoutedEventArgs e)
        {
            if (kaoshistatus == "已结束")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选计划状态不可进行取消审核通过操作！");
                return;
            }
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (GuaZhiEmpModel i in (ObservableCollection<GuaZhiEmpModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    if (i.Status == "未审核" || i.Status == "已退回" || i.Status == "终止")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选信息状态为不可取消审核状态！");
                        return;
                    }
                }
                if (MessageBox.Show("确定取消审核吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";

                    foreach (GuaZhiEmpModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    Client1.CancelGuaZhiEmpAsync("(" + ids.Substring(0, ids.Length - 1) + ")");
                }

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要通过审核的信息！");
        }

        private void TuiData_Click(object sender, RoutedEventArgs e)
        {
            if (kaoshistatus == "已结束")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选计划状态不可进行退回操作！");
                return;
            }
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (GuaZhiEmpModel i in (ObservableCollection<GuaZhiEmpModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    if (i.Status == "未审核" || i.Status == "已退回" || i.Status == "终止")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选信息状态为不可退回状态！");
                        return;
                    }
                }
                if (MessageBox.Show("确定退回吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    string ids = "";

                    foreach (GuaZhiEmpModel i in selectedempmodel)
                    {
                        ids += i.ID.ToString() + ",";
                    }
                    GuaZhiTuiHuiWindow win = new GuaZhiTuiHuiWindow("(" + ids.Substring(0, ids.Length - 1) + ")");
                    win.Closed += new EventHandler(win_Closed);
                    win.Show();
                }
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要退回的信息");
            }
        }

        void win_Closed(object sender, EventArgs e)
        {
            if (((ChildWindow)sender).DialogResult == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("操作成功！");
                string ss = "";
                foreach (string ii in orgids)
                    ss += ii + ",";
                if (string.IsNullOrEmpty(ss)) ss = "0";
                else ss = ss.Substring(0, ss.Length - 1);
                LoadData(ss);
            }
            
        }

        private void SavePeiZhiData_Click(object sender, RoutedEventArgs e)
        {
            if (kaoshistatus == "已结束")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选计划状态不可进行岗位安排操作！");
                return;
            }
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (GuaZhiEmpModel i in (ObservableCollection<GuaZhiEmpModel>) DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    if (i.Status == "未审核" || i.Status == "已退回" || i.Status == "终止")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选信息状态不可进行岗位安排操作！");
                        return;
                    }
                }
                //ObservableCollection<int> list = new ObservableCollection<int>();
                //foreach(GuaZhiEmpModel i in selectedempmodel)
                //{
                //    list.Add(i.ID);
                //}
                GuaZhiAnPaiWindow win = new GuaZhiAnPaiWindow(selectedempmodel);
                win.Closed+=new EventHandler(win_Closed);
                win.Show();
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要进行岗位安排操作的信息");
            }
        }

        private int count = 0;
        private void SaveShiJianData_Click(object sender, RoutedEventArgs e)
        {
            if (kaoshistatus == "已结束")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选计划状态不可进行岗位安排操作！");
                return;
            }
            count = 0;
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (GuaZhiEmpModel i in (ObservableCollection<GuaZhiEmpModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    if (i.Status == "审核通过")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选信息状态不可进行时间安排操作！");
                        return;
                    }
                }

                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    Client1.UpdateShiJianAsync(i);
                    count = count + 1;
                }
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要进行时间安排操作的信息");
            }
        }

        private void ImageButton_Click(object sender, RoutedEventArgs e)
        {
            if(ExamID != 0)
            {
                string ss = "";
                foreach (string ii in orgids)
                    ss += ii + ",";
                if (string.IsNullOrEmpty(ss)) ss = "0";
                else ss = ss.Substring(0, ss.Length - 1);
                GuaZhiEmpZhiWeiAnPaiControl view = new GuaZhiEmpZhiWeiAnPaiControl(ExamID, ss,"");
                //view.Nav = this.Nav;
                Nav.ShowNextPage("岗位安排情况", view, this);
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择一个计划进行查询");
            }
        }

        private void YanChang_Click(object sender, RoutedEventArgs e)
        {
            if (kaoshistatus == "已结束")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选计划状态不可进行延期申请操作！");
                return;
            }
            count = 0;
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (GuaZhiEmpModel i in (ObservableCollection<GuaZhiEmpModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    if (i.Status != "挂职中" || i.Status != "挂职中-延期")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选信息状态不可进行延期申请操作！");
                        return;
                    }
                }
                string list = "";
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    list += i.ID + ",";
                }
                GuaZhiYanQiWindow win = new GuaZhiYanQiWindow(list.Substring(0,list.Length - 1));
                win.Closed += new EventHandler(win_Closed);
                win.Show();
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要进行延期申请的信息");
            }
        }

        private void ShenHeYanChang_Click(object sender, RoutedEventArgs e)
        {
            if (kaoshistatus == "已结束")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选计划状态不可进行延期审核操作！");
                return;
            }
            count = 0;
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (GuaZhiEmpModel i in (ObservableCollection<GuaZhiEmpModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    if (i.Status != "挂职中-延期申请")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选信息状态不可进行延期审核操作！");
                        return;
                    }
                }
                string list = "";
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    list += i.ID + ",";
                }
                if (MessageBox.Show("确定要审核通过该延期申请码？", "提示",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    ShenHeYanQiWindow win = new ShenHeYanQiWindow(list.Substring(0, list.Length - 1));
                    win.Show();
                    win.Closed +=new EventHandler(win_Closed);
                }
                    
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要进行延期审核的信息");
            }
        }

        void Client1_ShenHeShenHeYanQiGuaZhiEmpCompleted(object sender, ShenHeShenHeYanQiGuaZhiEmpCompletedEventArgs e)
        {
            string ss = "";
            foreach (string ii in orgids)
                ss += ii + ",";
            if (string.IsNullOrEmpty(ss)) ss = "0";
            else ss = ss.Substring(0, ss.Length - 1);
            LoadData(ss);
        }

        private void Export_Click(object sender, RoutedEventArgs e)
        {
            if (kaoshistatus == "已结束")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选计划状态不可进行挂职终止操作！");
                return;
            }
            count = 0;
            selectedempmodel.Clear();
            if (DGRole.ItemsSource != null)
            {
                foreach (GuaZhiEmpModel i in (ObservableCollection<GuaZhiEmpModel>)DGRole.ItemsSource)
                {
                    if (i.IsSelected == true && selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() == null)
                    {
                        selectedempmodel.Add(i);
                    }
                    if (selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault() != null && i.IsSelected == false)
                    {
                        selectedempmodel.Remove(selectedempmodel.Where(p => p.ID == i.ID).FirstOrDefault());
                    }
                }
            }
            if (selectedempmodel.Count > 0)
            {
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    if (i.Status == "未审核" || i.Status == "已退回" || i.Status == "审核通过" || i.Status == "终止")
                    {
                        SilverlightMessageBoxLibrary.Message.ErrorMessage("所选信息状态不可进行挂职终止操作！");
                        return;
                    }
                }
                string list = "";
                foreach (GuaZhiEmpModel i in selectedempmodel)
                {
                    list += i.ID + ",";
                }
                GuaZhiZhongZhiWindow win = new GuaZhiZhongZhiWindow(list.Substring(0, list.Length - 1));
                win.Closed += new EventHandler(win_Closed);
                win.Show();
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要进行挂职终止的信息");
            }
        }
    }
}
